<!--
 @description 各部门销售开单明细数据
 @fileName index.vue
 @author zengqiongying
 @created 2021/05/28 16:15:24
-->
<template>
  <div>
    <div class="data-table">
      <div class="table-head">
        <div class="td">部门</div>
        <div class="td" @click="handleSort('dayQuantity')">今日
          <div
            class="icon-sort"
            :class="{
              'asc':sortKey ==='dayQuantity' && sortType==='asc',
              'desc':sortKey ==='dayQuantity' && sortType==='desc'
            }"
          />
        </div>
        <div class="td" @click="handleSort('monthQuantity')">本月
          <div
            class="icon-sort"
            :class="{
              'asc':sortKey ==='monthQuantity' && sortType==='asc',
              'desc':sortKey ==='monthQuantity' && sortType==='desc'
            }"
          />
        </div>
        <div class="td" @click="handleSort('yearQuantity')">本年
          <div
            class="icon-sort"
            :class="{
              'asc':sortKey ==='yearQuantity' && sortType==='asc',
              'desc':sortKey ==='yearQuantity' && sortType==='desc'
            }"
          />
        </div>
      </div>
      <div class="table-body">
        <div v-for="(item,index) in tableData" :key="index" class="tr">
          <div class="td">{{ item.officeName }}</div>
          <div class="td">{{ (item.dayQuantity || '-') | filterForamtThousand }}</div>
          <div class="td">{{ (item.monthQuantity || '-') | filterForamtThousand }}</div>
          <div class="td">{{ (item.yearQuantity || '-') | filterForamtThousand }}</div>
        </div>
      </div>
    </div>
    <!-- <div class="more" @click="handleMoreOffice">
      <template v-if="!loadover">
        查看更多办事处 <svg-icon icon-class="xiangxia" />
      </template>
      <template v-else>到底了，没有更多啦....</template>
    </div> -->
  </div>
</template>

<script>
import { getSaleOrderOffice } from '@/apis/sale'
export default {
  name: 'OfficeSaleOrderData',
  data() {
    return {
      page: 1,
      pageSize: 10,
      totalCount: 10,
      sortKey: '', // 排序字段
      sortType: '', // 排序：asc/desc
      tableOrgData: [], // 表格原数据
      tableData: [
        /* { name: '湖北办', day: 231, month: 3259, pre: 10, rate: 85.62, state: 1 },
        { name: '江苏办', day: 168, month: 2376, pre: 8, rate: 89.02, state: 0 },
        { name: '重庆办', day: 79, month: 852, pre: 9, rate: 90.45, state: 1 },
        { name: '甘肃办', day: 144, month: 1003, pre: 12, rate: 85.62, state: 1 },
        { name: '河北办', day: 99, month: 843, pre: 11, rate: 85.62, state: 1 } */
      ]
    }
  },
  computed: {
    loadover() {
      return this.totalCount < this.page * this.pageSize
    }
  },
  created() {
    this.getOfficeData()
  },
  methods: {
    getOfficeData() {
      getSaleOrderOffice().then(res => {
        let tableOrgData = []
        if (res.data && res.data.length) {
          tableOrgData = res.data.map(v => {
            const name = v.officeName.replace('直营办事处', '办')
              .replace('独资办事处', '办')
              .replace('销售公司', '')
            const item = { ...v, ...{ officeName: name }}
            return item
          })
        }
        this.tableOrgData = tableOrgData
        this.totalCount = this.tableOrgData.length
        this.handleSort('dayQuantity')
      })
    },
    handleMoreOffice() {
      if (this.totalCount < this.page * this.pageSize) return
      this.page += 1
      this.getOfficeData()
    },
    handleSort(field) {
      if (this.sortKey === field) {
        this.sortType = this.sortType === 'desc' ? 'asc' : 'desc'
      } else {
        this.sortKey = field
        this.sortType = 'desc'
      }
      const dataList = this.tableOrgData.sort((a, b) => {
        if (this.sortType === 'desc') {
          return Number(b[field] || 0) - Number(a[field] || 0)
        } else {
          return Number(a[field] || 0) - Number(b[field] || 0)
        }
      })
      this.tableData = dataList
    }
  }
}

</script>
<style lang='less' scoped>
  .data-table{
    color: #333333;
    text-align: center;
    letter-spacing: 1px;
    line-height: 20px;
    margin-bottom: 10px;
    .table-head{
      display: flex;
      align-items: center;
      height: 40px;
      background: #F5F6F7;
      border-radius: 6px;
      font-size: 14px;
    }
    .table-body{
      min-height: 37px;
      font-size: 12px;
    }
    .tr{
      display: flex;
      align-items: center;
      padding: 8px 0;
      border-bottom: 1px solid #F5F6F7;
    }
    .td{
      width: 25%;
      display: flex;
      justify-content: center;
      align-items: center;
      padding: 0 5px;
      overflow: hidden;
    }
    .icon-pre{
      width: 6px;
      height:7px;
      margin-left: 5px;
    }
    .icon-sort{
      width: 12px;
      height: 14px;
      margin-left: 5px;
      background-image:url('~@/assets/image/icon_sort.png');
      background-size: 12px 14px;
      background-repeat: no-repeat;
      &.asc{
        background-image:url('~@/assets/image/icon_sort_asc.png');
      }
      &.desc{
        background-image:url('~@/assets/image/icon_sort_desc.png');
      }
    }
  }
  .more{
    height: 26px;
    display: flex;
    justify-content: center;
    align-items: center;
    color: #888888;
    font-size: 12px;
    .svg-icon{
      font-size: 16px;
    }
  }
</style>
